package com.elric.mall.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.IdUtil;
import com.elric.mall.common.exception.ApiException;
import com.elric.mall.common.utils.ImportExcelUtil;
import com.elric.mall.dto.ZgBaseUpperUnitImportDto;
import com.elric.mall.enums.ImportType;
import com.elric.mall.mapper.ZgBaseUpperUnitMapper;
import com.elric.mall.model.ZgBaseUpperUnit;
import com.elric.mall.model.ZgBaseUpperUnitExample;
import com.elric.mall.service.ZgBaseUpperUnitService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 实现功能：大单位信息表实现层
 *
 * @author 作者[Yuxuan.he]
 * @version 1.0.00  2021-09-16 11:21
 */
@Service
public class ZgBaseUpperUnitServiceImpl implements ZgBaseUpperUnitService {
    @Autowired
    private ZgBaseUpperUnitMapper zgBaseUpperUnitMapper;

    @Override
    public int create(ZgBaseUpperUnit zgBaseUpperUnit) {
        zgBaseUpperUnit.setId(IdUtil.fastUUID());
        zgBaseUpperUnit.setCreateTime(new DateTime());
        zgBaseUpperUnit.setUpdateTime(new DateTime());
        return zgBaseUpperUnitMapper.insert(zgBaseUpperUnit);
    }

    @Override
    public int update(ZgBaseUpperUnit zgBaseUpperUnit) {
        zgBaseUpperUnit.setUpdateTime(new DateTime());
        return zgBaseUpperUnitMapper.updateByPrimaryKeySelective(zgBaseUpperUnit);
    }

    @Override
    public ZgBaseUpperUnit findById(String id) {
        return zgBaseUpperUnitMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(String id) {
        return zgBaseUpperUnitMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ZgBaseUpperUnit> list(Integer pageSize, Integer pageNum, ZgBaseUpperUnit zgBaseUpperUnit) {
        PageHelper.startPage(pageNum, pageSize);
        ZgBaseUpperUnitExample example = new ZgBaseUpperUnitExample();
        if(!StringUtils.isEmpty(zgBaseUpperUnit.getUnitName())) {
            example.createCriteria().andUnitNameLike("%"+zgBaseUpperUnit.getUnitName()+ "%");
        }
        example.setOrderByClause("order_num asc");
        return zgBaseUpperUnitMapper.selectByExample(example);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<ZgBaseUpperUnitImportDto> importDocuments(MultipartHttpServletRequest request) {
        return ImportExcelUtil.readExcelByRequest(request, new ZgBaseUpperUnitImportDto());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertBatch(List<ZgBaseUpperUnit> list, ImportType importType) {
        if (importType == ImportType.full) {
            ZgBaseUpperUnitExample example = new ZgBaseUpperUnitExample();
            zgBaseUpperUnitMapper.deleteByExample(example);
        }
        return zgBaseUpperUnitMapper.insertBatch(list);
    }
}
